package com.example.springboottest.example.function;

import org.thymeleaf.util.StringUtils;

import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class FunctionTest {
    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("Green", 150), new Apple("Green", 250),
                new Apple("Red", 150));

        total(apples);

//        Files.lin

//        List<Apple> filterApples = filterApple(apples, (ApplePredicate) (Apple apple) -> apple.getColor().equals("Green"));

        List<Apple> filterApples = filterApple(apples, new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                apple.setHeight(500);
                return apple.getColor().equals("Green");
            }

            public boolean test2(Apple apple) {
                return false;
            }
        });

        apples.get(0).setHeight(900);

//        for (Apple apple : apples)
//            System.out.println(apple.toString());
//
//        for (Apple apple : filterApples)
//            System.out.println(apple.toString());

        List<String> srcList = Arrays.asList("tom", "jack", "rose", "kate");
//        List<String> srcList = new ArrayList<String>();
//        srcList.add("tom");
//        srcList.add("jack");
//        srcList.add("rose");
//        srcList.add("kate");
//

//        srcList.remove("tom");
//        srcList.add("d");
//        srcList.add(0, "tt");
        srcList.stream().forEach(System.out::println);

//        Iterator<String> it = srcList.iterator();
//        while (it.hasNext()) {
//            if (StringUtils.equals(it.next(), "jack"))
//                it.remove();
//        }
//
//        srcList.stream().forEach(System.out::println);


        TestFunction testFunction = (Apple apple) -> {
            System.out.println("details:\t" + apple.toString());
        };
        testFunction.defaultFun();
        testFunction.showInfo(apples.get(0));

        test(apples.get(0), testFunction);
    }

    public static List<Apple> filterApple(List<Apple> apples, ApplePredicate predicate) {
        List<Apple> filterApples = new ArrayList<Apple>();
        for (Apple apple : apples) {
            if (predicate.test(apple))
                filterApples.add(apple);
        }
        return filterApples;
    }

    public static List<Apple> filterApple(List<Apple> apples, ApplePredicate2 predicate) {
        List<Apple> filterApples = new ArrayList<Apple>();
        for (Apple apple : apples) {
            if (predicate.test(apple))
                filterApples.add(apple);
        }
        return filterApples;
    }

    public static void total(List<Apple> apples) {
        int total = apples.stream().mapToInt(Apple::getHeight).sum();
        System.out.println("total=\t" + total);
    }

    public static void test(Apple apple, TestFunction testFunction) {
        testFunction.defaultFun();
        testFunction.showInfo(apple);
    }
}
